package com.hm.health.View;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;

import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.hm.health.R;
import com.hm.health.View.mpchart.LineChartEx;

import java.util.List;

/**
 * Created by RyanChen on 2015/12/18.
 */
public class LineCurveChartViewEx extends LineChartEx {
    private static final String TAG = "LineCurveChartView";
    private LineChartEx mChart;
    private Context mContext;
    private int mChannCount = 0;
    private int mPointInterval;
    private float mdYMin = 0.0f;
    private float mdYMax = 0.0f;
    private float mdYAverage = 0.0f;

    private final static int CHANNEL_COLOR[] = new int[3];

    static {
        CHANNEL_COLOR[0] = Color.RED;
        CHANNEL_COLOR[1] = Color.GREEN;
        CHANNEL_COLOR[2] = Color.YELLOW;
    }

    public int getPointInterval() {
        return mPointInterval;
    }

    public void setPointInterval(int mPointInterval) {
        this.mPointInterval = mPointInterval;
    }

    public LineCurveChartViewEx(Context context) {
        this(context,null);
    }

    public LineCurveChartViewEx(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public LineCurveChartViewEx(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mChart = this;
        mContext = context;
        preInitChart();
    }

    public void addLineDataSet(final String strChannName, int nColor) {
        mChart.getData().addDataSet(createHighLineDataSet(strChannName, nColor));
    }

    private void preInitChart() {
        mPointInterval = 2;
        mChart.setDescription("");
        mChart.setNoDataTextDescription("");
        mChart.setTouchEnabled(false);
        mChart.setDragEnabled(false);
        mChart.setScaleEnabled(false);
        mChart.setDrawGridBackground(false);
        mChart.setPinchZoom(false);
        mChart.setAutoScaleMinMaxEnabled(true);
        mChart.getXAxis().setDrawLabels(true);
        mChart.setTouchEnabled(false);

        Legend l = mChart.getLegend();
        l.setPosition(Legend.LegendPosition.BELOW_CHART_CENTER);
        l.setForm(Legend.LegendForm.LINE);
        l.setTextColor(Color.CYAN);
        l.setEnabled(true);

        XAxis xl = mChart.getXAxis();
        xl.setTextColor(getResources().getColor(R.color.light_sleep));
        xl.setDrawGridLines(true);
        xl.setAvoidFirstLastClipping(true);
        xl.setSpaceBetweenLabels(5);
        xl.setEnabled(true);
        xl.setPosition(XAxis.XAxisPosition.BOTTOM);

        YAxis leftAxis = mChart.getAxisLeft();
        leftAxis.setTextColor(getResources().getColor(R.color.light_sleep));
        leftAxis.setAxisMaxValue(250);
        leftAxis.setAxisMinValue(0);
        leftAxis.setStartAtZero(true);
        leftAxis.setDrawGridLines(true);
        leftAxis.setLabelCount(2, true);
        leftAxis.setEnabled(true);

        YAxis rightAxis = mChart.getAxisRight();
        rightAxis.setEnabled(false);

        LineData data = new LineData();
        data.setValueTextColor(Color.WHITE);
        mChart.setData(data);
    }

    public LineDataSet getPPGLineDataSet(int nChannel) {
        if (mChart.getData().getDataSetCount() > nChannel) {
            return getData().getDataSetByIndex(nChannel);
        } else {
            return null;
        }
    }

    public void addEntry(int nChannIndex, Entry entry) {
        if (nChannIndex > mChannCount) {
            return;
        }

        LineData data = mChart.getData();
        if (nChannIndex == 0) {
            data.addXValue(entry.getXIndex() + "");
        }
        data.addEntry(entry, nChannIndex);
        calcYValueMinMax();

        Log.d(TAG, "Max:" + mdYMax + " Min:" + mdYMin);
        mChart.getAxisLeft().setAxisMaxValue(mdYMax);
        mChart.getAxisLeft().setAxisMinValue(mdYMin);
        mChart.setVisibleXRangeMaximum(getMaxVisibleCount());
        mChart.moveViewTo(entry.getXIndex(), entry.getVal(), YAxis.AxisDependency.LEFT);
        mChart.notifyDataSetChanged();
    }

    private void calcYValueMinMax() {
        LineData data = mChart.getData();

        float dYMin = Float.MAX_VALUE;
        float dYMax = Float.MIN_VALUE;
        double dYAverage = 0;

        long lSize = 0;
        for (int nChannel = 0; nChannel < mChannCount; ++nChannel) {
            List<Entry> listEntry = data.getDataSets().get(nChannel).getYVals();
            lSize += listEntry.size();
            for (Entry e : listEntry) {
                if (e.getVal() > dYMax) {
                    dYMax = e.getVal();
                }

                if (e.getVal() < dYMin) {
                    dYMin = e.getVal();
                }

                dYAverage += e.getVal();
            }
        }

        mdYAverage = (float) (dYAverage / lSize);
        float dDiff =  (dYMax - dYMin);
        if (dDiff > Float.MIN_VALUE) {
            mdYMax = dYMax + dDiff / 3;
            mdYMin = dYMin - dDiff / 3;
        } else {
            mdYMax = dYMax + 100;
            mdYMin = dYMax - 100;
        }
    }

    /**
     * 清空当前通道所有数据
     *
     * @param nChannel
     */
    public void clearChannelData(int nChannel) {
        if (nChannel < mChannCount && nChannel >= 0) {
            getData().getDataSetByIndex(nChannel).clear();
        }
    }

    public LineDataSet createHighLineDataSet(final String strName, int nColor) {
        LineDataSet set = new LineDataSet(null, strName);
        set.setAxisDependency(YAxis.AxisDependency.LEFT);
        set.setColor(nColor);
        set.setLineWidth(2f);
        set.setCircleColor(getResources().getColor(R.color.light_sleep));
        set.setCircleSize(4f);
        set.setFillAlpha(128);
        set.setCircleColorHole(getResources().getColor(R.color.light_sleep));
        set.setHighLightColor(Color.GREEN);
        set.setValueTextColor(Color.RED);
        set.setValueTextSize(10f);
        set.setDrawValues(true);
        set.setDrawCircles(true);
        set.setDrawCircleHole(false);
        set.setDrawFilled(false);
        set.setHighLightColor(getResources().getColor(R.color.light_sleep));
        set.setHighlightEnabled(false);
//        set.setDrawCubic(true);
        mChannCount++;
        return set;
    }
}
